// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk.Query
{
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk.Temporary;

	[DataContract(Name = "FilterExpression", Namespace = XmlNamespaces.V5.Contracts)]
	public sealed class FilterExpression : IExtensibleDataObject
	{
		public FilterExpression()
		{
		}

		public FilterExpression(LogicalOperator filterOperator)
		{
			this.FilterOperator = filterOperator;
		}

		[DataMember]
		public LogicalOperator FilterOperator
		{
			get
			{
				return _filterOperator;
			}
			set
			{
				_filterOperator = value;
			}
		}

		[DataMember]
		public DataCollection<ConditionExpression> Conditions
		{
			get
			{
				if (_conditions == null)
				{
					_conditions = new DataCollection<ConditionExpression>();
				}
				return _conditions;
			}
			[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called via reflection")]
			private set
			{
				_conditions = value;
			}
		}

		[DataMember]
		public DataCollection<FilterExpression> Filters
		{
			get
			{
				if (_filters == null)
				{
					_filters = new DataCollection<FilterExpression>();
				}
				return _filters;
			}
			[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called via reflection")]
			private set
			{
				_filters = value;
			}
		}

		/// <summary>
		/// overloaded add multiple value condition to the filter 
		/// </summary>
		/// <param name="attributeName"></param>
		/// <param name="conditionOperator"></param>
		/// <param name="values"></param>
		/// <returns></returns>
		public void AddCondition(
			string attributeName,
			ConditionOperator conditionOperator,
			params object[] values)
		{
			Conditions.Add(new ConditionExpression(
				attributeName,
				conditionOperator,
				values));
		}

		public void AddCondition(ConditionExpression condition)
		{
			Conditions.Add(condition);
		}

		/// <summary>
		/// add filter to current filter
		/// </summary>
		/// <param name="logicalOperator"></param>
		/// <returns></returns>
		public FilterExpression AddFilter(LogicalOperator logicalOperator)
		{
			FilterExpression filter = new FilterExpression();
			filter.FilterOperator = logicalOperator;
			Filters.Add(filter);
			return filter;
		}

		/// <summary>
		/// add filter to current filter
		/// </summary>
		/// <param name="childFilter">Filter to be added.</param>
		/// <returns></returns>
		public void AddFilter(FilterExpression childFilter)
		{
			if (null != childFilter)
			{
				Filters.Add(childFilter);
			}
		}

		internal void Accept(IQueryVisitor visitor)
		{
			visitor.Visit(this);
		}

		private LogicalOperator _filterOperator;
		private DataCollection<ConditionExpression> _conditions;
		private DataCollection<FilterExpression> _filters;

		#region IExtensibleDataObject Implementation
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}
